package com.serve.oa.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;


import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dl")
public class ProxyController {

    private final WebClient webClient;

    @Autowired
    public ProxyController(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder
                .baseUrl("http://10.100.101.104/ppfmgr")
                .filter(this::logRequest)
                .build();
    }

    private Mono<ClientResponse> logRequest(ClientRequest request, ExchangeFunction next) {
        System.out.println("Request URI:22 " + request.url());
        System.out.println("Request Method: " + request.method());
        System.out.println("Request Headers: " + request.headers());
        return next.exchange(request);
    }


    @RequestMapping(value = "/**", method = {RequestMethod.GET, RequestMethod.POST})
    public Mono<ResponseEntity<String>> proxyRequest(

            @RequestBody(required = false) String body,
            @RequestHeader HttpHeaders headers,
            HttpServletRequest request) {

        System.out.println(request.getRequestURI());
        System.out.println(21313);
        URI uri = UriComponentsBuilder.fromHttpUrl("http://10.100.101.104/ppfmgr"+ request.getRequestURI().substring("/api/dl".length()))
                .query(request.getQueryString())
                .build(true)
                .toUri();




        // 将 RequestMethod 转换为 HttpMethod
        HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());


        WebClient.RequestBodySpec requestSpec = webClient.method(httpMethod)
                .uri(uri)
                .header(HttpHeaders.COOKIE, "mgr.session.id=996fceedf2db42038670a57dbddc7a89");

        if (body != null) {
            requestSpec.bodyValue(body);
        }

        // 添加传入的请求头
        requestSpec.headers(httpHeaders -> httpHeaders.addAll(headers));

        return requestSpec.retrieve()
                .toEntity(String.class);
    }

    private String buildQueryString(Map<String, String> queryParams) {
        if (queryParams.isEmpty()) {
            return "";
        }
        return "?" + queryParams.entrySet().stream()
                .map(entry -> entry.getKey() + "=" + entry.getValue())
                .collect(Collectors.joining("&"));
    }


}
